package com.erp.order.purchasing.controller;

import com.erp.hr.base.controller.BaseController;
import com.erp.hr.base.vo.ResultVO;
import com.erp.order.purchasing.model.PurchaseOrder;
import com.erp.order.purchasing.service.PurchaseOrderService;
import org.redragon.framework.hibernate.model.Pages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 采购订单 控制层
 * @Author: syz
 * @blame syz
 * @CreateTime: 2021/4/6
 */
@Controller
@RequestMapping("web/purchaseOrder")
public class PurchaseOrderController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PurchaseOrderController.class);

    /**
     * 采购订单服务
     */
    @Autowired
    private PurchaseOrderService purchaseOrderService;


    /**
     * 获取列表分页数据
     * @param pages 分页对象
     * @param purchaseOrder 采购订单对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("getPageList")
    public String getPageList(Pages pages, PurchaseOrder purchaseOrder, Model model) {
        model.addAttribute("pages", setPages(pages));
        model.addAttribute("list", purchaseOrderService.addCompleteData(purchaseOrderService.getDataObjectsForDataAuth("", pages, purchaseOrder)));
        model.addAttribute("statusMap", purchaseOrderService.getDictByKey("purchase_order_status"));
        return "basic.jsp?content=purchaseOrder/list";
    }

    /**
     * 设置订单金额
     * @param purchaseOrder 采购订单对象
     * @return 设置结果
     */
    @ResponseBody
    @RequestMapping("setMoney")
    public ResultVO<String> setMoney(PurchaseOrder purchaseOrder) {
        return purchaseOrderService.setMoney(purchaseOrder);
    }

    /**
     * 更新订单状态
     * @param id 采购订单id
     * @return 更新结果
     */
    @ResponseBody
    @RequestMapping("changeStatus")
    public ResultVO<String> changeStatus(String id) {
        return purchaseOrderService.changeStatus(id);
    }

    /**
     * 跳转至详情页
     * @param purchaseOrder 采购订单对象
     * @param model 模型对象
     * @return 页面路径
     */
    @RequestMapping("detail")
    public String detail(PurchaseOrder purchaseOrder, Model model) {
        model.addAttribute("purchaseOrder",
                purchaseOrderService.addCompleteData(
                        purchaseOrderService.getDataObject(
                                purchaseOrder.getId().toString())));
        model.addAttribute("unitMap", purchaseOrderService.getDictByKey("material_unit"));
        model.addAttribute("subStatusMap", purchaseOrderService.getDictByKey("requisition_detail_status"));
        return "basic.jsp?content=purchaseOrder/detail";
    }

}